package it.fuesi.bubblekids.views;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.Preference;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import it.fuesi.bubblekids.R;


/**
 * Created by pascotto on 10/11/14.
 */
public class SeekBarPreference extends Preference implements SeekBar.OnSeekBarChangeListener{

    private static final String TAG = "SeekBarPreference";

    private SeekBar mSeek;
    private TextView mSeekValue;
    private TextView mSeekTitle;
    private int mValue;
    private int minValue = 0;
    private int maxValue = 100;

    public int getMinValue() {
        return minValue;
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
        if(mSeek != null)
            mSeek.setMax(this.maxValue);
    }

    public SeekBarPreference(Context context){
        super(context);
    }

    public SeekBarPreference(Context context, AttributeSet attrs){
        super(context, attrs);

    }

    @Override
    public void setDefaultValue(Object defaultValue) {
        super.setDefaultValue(defaultValue);

        if(mValue == 0)
            mValue = Integer.parseInt(""+defaultValue);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        Log.d(TAG, "OnBindView --> ["+getKey()+"] -- value["+mValue+"] - title["+getTitle()+"]");

        mSeekValue = (TextView)view.findViewById(R.id.seekbar_value);
        mSeekValue.setText(""+mValue);
        mSeekTitle = (TextView)view.findViewById(R.id.seekbar_title);
        mSeekTitle.setText(getTitle());
        mSeek = (SeekBar)view.findViewById(R.id.seekbar);
        mSeek.setOnSeekBarChangeListener(this);

        mSeek.setMax(maxValue);
        mSeek.setProgress(mValue);


    }


    @Override
    protected View onCreateView(ViewGroup parent) {
        Resources res=getContext().getResources();
        View v=super.onCreateView(parent);
        v.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, res.getDimensionPixelSize(R.dimen.rowheight)));
        return v;
    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
        if(progress < minValue) progress = minValue;
        if(fromUser)
            refreshValue(progress);
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        if(seekBar.getProgress() < minValue) seekBar.setProgress(minValue);
        refreshValue(seekBar.getProgress());
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }


    private void refreshValue(int progress){
        mValue = progress;
        mSeekValue.setText(""+mValue);

        persistInt(mValue);

        Log.d(TAG, "refreshValue --> ["+getKey()+"] - value["+mValue+"]");


    }

    @Override
    protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
        Log.d(TAG, "onSetInitialValue --> ["+getKey()+"] - defaultValue["+defaultValue+"]");

        if (restorePersistedValue) {
            // Restore existing state
            mValue = this.getPersistedInt(Integer.parseInt(defaultValue != null ? ""+ defaultValue : "0"));
        } else {
            // Set default state from the XML attribute
            mValue = (Integer) defaultValue;
        }

        if(mValue < minValue) mValue = minValue;

        Log.d(TAG, "onSetInitialValue --> " + mValue);

       // mSeek.setProgress(mValue);


    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Log.d(TAG, "onSaveInstanceState");
        final Parcelable superState = super.onSaveInstanceState();
        // Check whether this Preference is persistent (continually saved)
        if (isPersistent()) {
            // No need to save instance state since it's persistent,
            // use superclass state
            return superState;
        }

        // Create instance of custom BaseSavedState
        final SavedState myState = new SavedState(superState);
        // Set the state's value with the class member that holds current
        // setting value
        Log.d(TAG, "onSaveInstanceState --> " + mValue);

        myState.value = mValue;
        return myState;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        Log.d(TAG, "onRestoreInstanceState");
        // Check whether we saved the state in onSaveInstanceState
        if (state == null || !state.getClass().equals(SavedState.class)) {
            // Didn't save the state, so call superclass
            super.onRestoreInstanceState(state);
            return;
        }

        // Cast state to custom BaseSavedState and pass to superclass
        SavedState myState = (SavedState) state;
        super.onRestoreInstanceState(myState.getSuperState());

        // Set this Preference's widget to reflect the restored state

        Log.d(TAG, "onRestoreInstanceState --> " + myState.value);

       mSeek.setMax(myState.value);



    }


    private static class SavedState extends BaseSavedState {
        // Member that holds the setting's value
        // Change this data type to match the type saved by your Preference
        int value;

        public SavedState(Parcelable superState) {
            super(superState);
        }

        public SavedState(Parcel source) {
            super(source);
            // Get the current preference's value
            value = source.readInt();  // Change this to read the appropriate data type
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            super.writeToParcel(dest, flags);
            // Write the preference's value
            dest.writeInt(value);  // Change this to write the appropriate data type
        }

        // Standard creator object using an instance of this class
        public static final Creator<SavedState> CREATOR =
                new Creator<SavedState>() {

                    public SavedState createFromParcel(Parcel in) {
                        return new SavedState(in);
                    }

                    public SavedState[] newArray(int size) {
                        return new SavedState[size];
                    }
                };
    }

}
